/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager.helpers.table;

import java.text.Collator;
import java.util.Comparator;
import javax.swing.table.TableRowSorter;
import manager.auxiliares.Format;

/**
 *
 * @author Thiago
 */
public class TableRowComparator extends TableRowSorter<javax.swing.table.TableModel> {

    public TableRowComparator() {
        super();
    }

    @Override
    public Comparator<?> getComparator(int column) {
        Class columnClass = getModel().getColumnClass(column);
        if (columnClass == String.class) {
            return Collator.getInstance();
        } else if (columnClass == Boolean.class) {
            return compareBoolean();
        } else if (columnClass == Double.class) {
            return compareDouble();
        } else if (columnClass == Integer.class) {
            return compareInteger();
        }
        Comparator comparator = super.getComparator(column);
        if (comparator != null) {
            return comparator;
        }
        return Collator.getInstance();
    }

    public static Comparator<String> compareInteger() {
        Comparator<String> comparator = new Comparator<String>() {

            public int compare(String s1, String s2) {
                Integer valor1 = null;
                Integer valor2 = null;
                int resposta = 0;
                try {
                    if (Format.isInteger(s1) && Format.isInteger(s2)) {
                        //System.out.println(s1+"    "+s2);
                        valor1 = Integer.parseInt(s1);
                        valor2 = Integer.parseInt(s2);
                        resposta = valor1.compareTo(valor2);
                    } else {
                        resposta = -1;
                    }
                } catch (Exception e) {
                }
                return resposta;
            }
        };
        return comparator;
    }

    public static Comparator<String> compareDouble() {
        Comparator<String> comparator = new Comparator<String>() {

            public int compare(String s1, String s2) {
                Double valor1 = null;
                Double valor2 = null;
                int resposta = 0;
                try {
                    if (Format.isDouble(s1) && Format.isDouble(s2)) {
                        valor1 = Double.parseDouble(s1);
                        valor2 = Double.parseDouble(s2);
                        resposta = valor1.compareTo(valor2);
                    } else {
                        resposta = -1;
                    }
                } catch (Exception e) {
                }
                return resposta;
            }
        };
        return comparator;
    }

    public static Comparator<Boolean> compareBoolean() {
        Comparator<Boolean> comparator = new Comparator<Boolean>() {

            public int compare(Boolean s1, Boolean s2) {
                Double valor1 = null;
                Double valor2 = null;
                int resposta = 0;
                return resposta;
            }
        };
        return comparator;
    }
}
